<script type="text/javascript">
    $(document).ready(function () {

        hideContainer();
         
        $('#saveGroup').click(function (e) {
            $('#frmGroup').submit();
        })
        
        $('#newGroupButton').click(function(){
            $('.divContainer').hide('slow');
            $('.buttonsSelectors').show('slow');
            $(this).hide();
            $('#newGroup').show('slow');
        })
        $('#listGroupButton').click(function(){
            $('.divContainer').hide('slow');
            $('.buttonsSelectors').show('slow');
            $(this).hide();
            $('#groupList').show('slow');
        })
    });
    
    function hideContainer(){
        $('.divContainer').hide();
        $('#listGroupButton').hide();
        $('#groupList').show();
    }
    
</script>
<div class="content-box">
    <div class="content-header">
        <h3>Grupos</h3>
    </div>
    
    <a href="javascript:void(0)" id="newGroupButton" class="buttonsSelectors"><span><span>Nuevo Grupo</span></span></a>
    <a href="javascript:void(0)" id="listGroupButton" class="buttonsSelectors"><span><span>Ver Grupos</span></span></a>

    <?php if ($this->message): ?>
        <div class="notice">
            <?php echo $this->message; ?>
        </div>
    <?php endif; ?>

    <div class="error" style="display: none">
    </div>
    <div id="newGroup" class="divContainer">
        <form id="frmGroup" action="/group/save" method="post">
            <div>
                <label>Nombre de Grupo</label>
                <input type="text" name="name" size="30" class="required"/>
            </div>
            <p>Seleccione amigos para el grupo:</p>

            <div id="friendList">
                <?php if (count($this->friends) != 0) : ?>
                    <table style="width: 100%">
                        <thead>
                        <th>Nombre</th>
                        <th>Apellido</th>
                        <th></th>
                        </thead>                

                        <?php foreach ($this->friends as $friend): ?>
                            <?php if ($friend->status == Friendship::ARE_FRIEND): ?>
                                <?php $friendData = User::findOneById($friend->id_friend) ?>
                                <tr>
                                    <td><?php echo $friendData->profile->name; ?></td>
                                    <td><?php echo $friendData->profile->surname; ?></td>
                                    <td><input type="checkbox" name="friends[]" value="<?php echo $friendData->id; ?>"/></td>
                                </tr>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </table>
                <?php else: ?>
                    <h3>No hay Amigos</h3>
                <?php endif; ?>

            </div>
            <p class="link">
                <a href="javascript:void(0)" id="saveGroup"><span><span>Crear Grupo</span></span></a>
            </p>
        </form>
    </div>
    
    <div id="editGroup" class="divContainer">
        <form id="frmGroupEdit" action="/group/update" method="post">
            <div>
                <label>Nombre de Grupo</label>
                <input type="text" name="name" size="30" class="required"/>
            </div>
            <p>Seleccione amigos para el grupo:</p>

            <div id="friendList">
                <?php if (count($this->friends) != 0) : ?>
                    <table style="width: 100%">
                        <thead>
                        <th>Nombre</th>
                        <th>Apellido</th>
                        <th></th>
                        </thead>                

                        <?php foreach ($this->friends as $friend): ?>
                            <?php if ($friend->status == Friendship::ARE_FRIEND): ?>
                                <?php $friendData = User::findOneById($friend->id_friend) ?>
                                <tr>
                                    <td><?php echo $friendData->profile->name; ?></td>
                                    <td><?php echo $friendData->profile->surname; ?></td>
                                    <td><input type="checkbox" name="friends[]" value="<?php echo $friendData->id; ?>"/></td>
                                </tr>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </table>
                <?php else: ?>
                    <h3>No hay Amigos</h3>
                <?php endif; ?>

            </div>
            <p class="link">
                <a href="javascript:void(0)" id="updateGroup"><span><span>Actualizar Grupo</span></span></a>
            </p>
        </form>
    </div>
    <div id="groupList" class="divContainer">
        <h2>Grupos</h2>
        <?php if(count($this->groups) != 0): ?>
            <table style="width: 100%">
                <thead>
                <th>Nombre de grupo</th>
                <th></th>
                <th></th>
                </thead>                

                <?php foreach ($this->groups as $userGroup): ?>
                    <tr>
                        <td><?php echo $userGroup->name; ?></td>
                        <td><a class="deleteLink" href="/group/deletegroup/id/<?php echo $userGroup->id; ?>">Eliminar Grupo</a></td>
                        <td><a class="editLink" href="/group/editgroup/id/<?php echo $userGroup->id; ?>">Editar Grupo</a></td>
                    </tr>
                <?php endforeach; ?>
            </table>
        <?php else: ?>
            No hay Grupos
        <?php endif; ?>
    </div>

    <div style="clear: both"></div>
</div>
